package com.atlin.project.service.impl;


import com.atlin.project.common.ErrorCode;
import com.atlin.project.contant.CommonConstant;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysAnnouncement;
import com.atlin.project.model.dto.sysAnnouncement.SysAnnouncementQueryRequest;
import com.atlin.project.model.vo.sysAnnouncement.SysAnnouncementVO;
import com.atlin.project.service.SysAnnouncementService;
import com.atlin.project.mapper.SysAnnouncementMapper;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description 针对表【sys_announcement(系统通知表)】的数据库操作Service实现
 * @createDate 2024-11-19 20:59:02
 */
@Service
public class SysAnnouncementServiceImpl extends ServiceImpl<SysAnnouncementMapper, SysAnnouncement>
        implements SysAnnouncementService {

    @Override
    public void valid(SysAnnouncement sysAnnouncement, boolean add) {
        String title = sysAnnouncement.getTitle();
        String content = sysAnnouncement.getContent();
        ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content), ErrorCode.PARAMS_ERROR, "标题 内容不能为空");
            
    }

    @Override
    public SysAnnouncementVO getSysAnnouncementVO(SysAnnouncement sysAnnouncement, HttpServletRequest request) {
        return SysAnnouncementVO.objToVo(sysAnnouncement);
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param sysAnnouncementQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<SysAnnouncement> getQueryWrapper(SysAnnouncementQueryRequest sysAnnouncementQueryRequest) {
        QueryWrapper<SysAnnouncement> queryWrapper = new QueryWrapper<>();
        if (sysAnnouncementQueryRequest == null) {
            return queryWrapper;
        }
        Long id = sysAnnouncementQueryRequest.getId();
        String title = sysAnnouncementQueryRequest.getTitle();
        String content = sysAnnouncementQueryRequest.getContent();
        Long userId = sysAnnouncementQueryRequest.getUserId();
        String sortField = sysAnnouncementQueryRequest.getSortField();
        String sortOrder = sysAnnouncementQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public Page<SysAnnouncementVO> getSysAnnouncementVOPage(Page<SysAnnouncement> sysAnnouncementPage, HttpServletRequest request) {
        List<SysAnnouncement> sysAnnouncementList = sysAnnouncementPage.getRecords();
        Page<SysAnnouncementVO> sysAnnouncementVOPage = new Page<>(sysAnnouncementPage.getCurrent(), sysAnnouncementPage.getSize(), sysAnnouncementPage.getTotal());
        if (CollectionUtils.isEmpty(sysAnnouncementList)) {
            return sysAnnouncementVOPage;
        }

        // 填充信息
        List<SysAnnouncementVO> sysAnnouncementVOList = sysAnnouncementList.stream().map(SysAnnouncementVO::objToVo).collect(Collectors.toList());
        sysAnnouncementVOPage.setRecords(sysAnnouncementVOList);
        return sysAnnouncementVOPage;
    }

}




